package com.zh.note.huawei.onehundred.计算二维矩阵的最大值;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // String[] lines = new String[n];
        // for (int i = 0; i < lines.length; i++) {
        //     lines[i] = sc.nextLine();
        // }
        int n = 5;
        String[] lines = {"1,0,0,0,1", "0,0,0,1,1", "0,1,0,1,0", "1,0,0,1,1", "1,0,1,0,1"};
        System.out.println(solution(n, lines));
    }

    private static int solution(int n, String[] lines) {
        int res = 0;
        int[][] nums = new int[n][];

        // 转化成二维整型数组
        for (int i = 0; i < n; i++) {
            nums[i] = Arrays.stream(lines[i].split(","))
                    .mapToInt(Integer::parseInt).toArray();

        }
        int m = nums[0].length;
        //
        for (int i = 0; i < n; i++) {
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < m; j++) {
                int[] shifted = new int[m];
                //从j开始复制，复制的长度是len-j
                System.arraycopy(nums[i], j, shifted, 0, m - j);
                System.arraycopy(nums[i], 0, shifted, m - j, j);
                String string = Arrays.toString(shifted);
                //\W 与 \w 相反，表示这个位置的字符既不是字母、数字，也不是下划线。
                // 把不是数字的字符全部替换成空
                String binInt = string.replaceAll("\\W+", "");
                int sum = Integer.parseInt(binInt, 2);
                if (sum > max) {
                    max = sum;
                }
            }
            res += max;
        }
        return res;
    }
}
